//
//  musicPlayerCenter.m
//  音乐播放器
//
//  Created by xalo on 16/1/4.
//  Copyright © 2016年 zhiying.com. All rights reserved.
//

#import "musicPlayerCenter.h"

@implementation musicPlayerCenter
{
    //将timer定于为这个类的全局变量
    NSTimer * _timer;
    //正在播放的网址
    NSString *_currentUrl;
}
- (void)stop{
    [self.player pause];
    [_timer invalidate];//取消计时器
    _timer = nil;
}
+(musicPlayerCenter *)defaultCenter{
   
    static musicPlayerCenter *center =nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        center = [[musicPlayerCenter alloc]init];
        center.player = [[AVPlayer alloc]init];
    });
  
    return center;
}

-(BOOL)isPlaying{
    //rate是当前播放器的播放速率 1代表证在播放 0代表没播放
    if (self.player.rate == 1) {
        return YES;
    }
    return NO;
}

- (void)play{
    [self.player play];
    
    //把原来的计时器置空
    [_timer invalidate];//让计时器无效
    _timer = nil;//让计时器为空
    //开始播放之后开启定海器向外面传参数
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];

}
//定时器的方法
- (void)timeRun{
    //安全判断
    if (self.timeBlock) {
        if (self.player.currentItem.duration.timescale!= 0) {
            // 计算当前时间
            CGFloat current = self.player.currentTime.value/self.player.currentTime.timescale;
            NSLog(@"当前时间%f",current);
            //计算总时间
            CGFloat total = self.player.currentItem.duration.value/self.player.currentItem.duration.timescale;
            //回调block
            self.timeBlock(current,total);
        }
        
    }
}

- (void)playWithUrlString:(NSString *)urlString{
    //如果网址和正在播放的网址相同，不执行下面播放 立即返回
    if ([_currentUrl isEqualToString:urlString]) {
        return;
    }
    _currentUrl = urlString;
    if (self.player.currentItem != nil) {
        //移除观察者
        [self.player.currentItem removeObserver:self forKeyPath:@"status" context:nil];
    }
    //根据网址创建一个播放媒体资源
    AVPlayerItem * item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:urlString]];
    //替换播放器的当前资源
    [self.player replaceCurrentItemWithPlayerItem:item];
    //观察当前资源的缓冲状态
    [self.player.currentItem addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:nil];
    //开始播放
   // [self play];
}


//- (void)Observer:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context{
//    NSLog(@"lkadlskcf");
//}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    AVPlayerItem *item = object;
    switch (item.status) {
        case AVPlayerItemStatusFailed:
            NSLog(@"缓存失败");
            break;
        case AVPlayerItemStatusUnknown:{
            NSLog(@"未知错误");
            UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"提示：" message:@"请检查网络" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
            
            break;
        case AVPlayerItemStatusReadyToPlay:
            NSLog(@"缓存成功");
            [self play];
//            //移除观察者
//            [self.player.currentItem removeObserver:self forKeyPath:@"status" context:nil];
            break;
            
        default:
            break;
        
    }
}
@end
